#include <stdio.h>
#include <stdlib.h>
#include "AVL.h"


int main() 
{
  AVLNode * root = NULL;    
  
  int ins[]={16, 10, 21, 5, 12, 18, 24, 2, 8, 11, 15, 19, 23, 31, 1, 6, 9, 13, 22, 7};
  int len = 20, x;
  for(int i=0; i<len; i++){
	  printf("\nAfter inserting key %d..\n", ins[i]);
	  insertNode(&root,ins[i]);
	  printTree(root);
  }
  printf("\nInsert a new node (+ to insert, - to delete, 0 to exit): ");
  scanf("%d",&x);
  while(x){
	  if(x > 0 )
		  insertNode(&root,x);
	  else
		  free(deleteNode(&root,-x));
	  printTree(root);
	  printf("\nInsert a new node (+ to insert, - to delete, 0 to exit): ");
	  scanf("%d",&x);
  }
  
  destroyTree(&root);

  return 0;
}


/******************** sample output

After inserting key 16..
16(0)
--------------------

After inserting key 10..
  16(1)
   /
10(0)
--------------------

After inserting key 21..
    16(1)
     / \
    /   \
   /     \
10(0)   21(0)
--------------------

After inserting key 5..
     16(2)
      / \
     /   \
    /     \
 10(1)   21(0)
  /
5(0)
--------------------

After inserting key 12..
       16(2)
        / \
       /   \
      /     \
   10(1)   21(0)
    / \
   /   \
  /     \
5(0)   12(0)
--------------------

After inserting key 18..
        16(2)
         / \
        /   \
       /     \
      /       \
   10(1)     21(1)
    / \       /
   /   \   18(0)
  /     \
5(0)   12(0)
--------------------

After inserting key 24..
           16(2)
            / \
           /   \
          /     \
         /       \
        /         \
       /           \
      /             \
   10(1)           21(1)
    / \             / \
   /   \           /   \
  /     \         /     \
5(0)   12(0)   18(0)   24(0)
--------------------

After inserting key 2..
             16(3)
              / \
             /   \
            /     \
           /       \
          /         \
         /           \
        /             \
     10(2)           21(1)
      / \             / \
     /   \           /   \
    /     \         /     \
  5(1)   12(0)   18(0)   24(0)
  /
2(0)
--------------------

After inserting key 8..
               16(3)
                / \
               /   \
              /     \
             /       \
            /         \
           /           \
          /             \
       10(2)           21(1)
        / \             / \
       /   \           /   \
      /     \         /     \
    5(1)   12(0)   18(0)   24(0)
    / \
   /   \
  /     \
2(0)   8(0)
--------------------

After inserting key 11..
               16(3)
                / \
               /   \
              /     \
             /       \
            /         \
           /           \
        10(2)         21(1)
         / \           / \
        /   \         /   \
       /     \       /     \
      /       \   18(0)   24(0)
    5(1)     12(1)
    / \       /
   /   \   11(0)
  /     \
2(0)   8(0)
--------------------

After inserting key 15..
                  16(3)
                   / \
                  /   \
                 /     \
                /       \
               /         \
              /           \
           10(2)         21(1)
            / \           / \
           /   \         /   \
          /     \       /     \
         /       \   18(0)   24(0)
        /         \
       /           \
      /             \
    5(1)           12(1)
    / \             / \
   /   \           /   \
  /     \         /     \
2(0)   8(0)    11(0)   15(0)
--------------------

After inserting key 19..
                  16(3)
                   / \
                  /   \
                 /     \
                /       \
               /         \
              /           \
           10(2)         21(2)
            / \           / \
           /   \         /   \
          /     \       /     \
         /       \   18(1)   24(0)
        /         \     \
       /           \   19(0)
      /             \
    5(1)           12(1)
    / \             / \
   /   \           /   \
  /     \         /     \
2(0)   8(0)    11(0)   15(0)
--------------------

After inserting key 23..
                     16(3)
                      / \
                     /   \
                    /     \
                   /       \
                  /         \
                 /           \
                /             \
               /               \
              /                 \
           10(2)               21(2)
            / \                 / \
           /   \               /   \
          /     \             /     \
         /       \           /       \
        /         \         /         \
       /           \     18(1)       24(1)
      /             \       \         /
    5(1)           12(1)   19(0)   23(0)
    / \             / \
   /   \           /   \
  /     \         /     \
2(0)   8(0)    11(0)   15(0)
--------------------

After inserting key 31..
                   16(3)
                    / \
                   /   \
                  /     \
                 /       \
                /         \
               /           \
              /             \
           10(2)           21(2)
            / \             / \
           /   \           /   \
          /     \         /     \
         /       \       /       \
        /         \   18(1)     24(1)
       /           \     \       / \
      /             \   19(0)   /   \
    5(1)           12(1)       /     \
    / \             / \     23(0)   31(0)
   /   \           /   \
  /     \         /     \
2(0)   8(0)    11(0)   15(0)
--------------------

After inserting key 1..
                     16(4)
                      / \
                     /   \
                    /     \
                   /       \
                  /         \
                 /           \
                /             \
             10(3)           21(2)
              / \             / \
             /   \           /   \
            /     \         /     \
           /       \       /       \
          /         \   18(1)     24(1)
         /           \     \       / \
        /             \   19(0)   /   \
      5(2)           12(1)       /     \
      / \             / \     23(0)   31(0)
     /   \           /   \
    /     \         /     \
  2(1)   8(0)    11(0)   15(0)
  /
1(0)
--------------------

After inserting key 6..
                     16(4)
                      / \
                     /   \
                    /     \
                   /       \
                  /         \
                 /           \
                /             \
             10(3)           21(2)
              / \             / \
             /   \           /   \
            /     \         /     \
           /       \       /       \
          /         \   18(1)     24(1)
         /           \     \       / \
        /             \   19(0)   /   \
      5(2)           12(1)       /     \
      / \             / \     23(0)   31(0)
     /   \           /   \
    /     \         /     \
  2(1)   8(1)    11(0)   15(0)
  /       /
1(0)    6(0)
--------------------

After inserting key 9..
                     16(4)
                      / \
                     /   \
                    /     \
                   /       \
                  /         \
                 /           \
                /             \
             10(3)           21(2)
              / \             / \
             /   \           /   \
            /     \         /     \
           /       \       /       \
          /         \   18(1)     24(1)
         /           \     \       / \
        /             \   19(0)   /   \
      5(2)           12(1)       /     \
      / \             / \     23(0)   31(0)
     /   \           /   \
    /     \         /     \
  2(1)   8(1)    11(0)   15(0)
  /       / \
1(0)     /   \
        /     \
      6(0)   9(0)
--------------------

After inserting key 13..
                     16(4)
                      / \
                     /   \
                    /     \
                   /       \
                  /         \
                 /           \
                /             \
             10(3)           21(2)
              / \             / \
             /   \           /   \
            /     \         /     \
           /       \       /       \
          /         \   18(1)     24(1)
         /           \     \       / \
        /             \   19(0)   /   \
      5(2)           12(2)       /     \
      / \             / \     23(0)   31(0)
     /   \           /   \
    /     \         /     \
  2(1)   8(1)    11(0)   15(1)
  /       / \             /
1(0)     /   \         13(0)
        /     \
      6(0)   9(0)
--------------------

After inserting key 22..
                      16(4)
                       / \
                      /   \
                     /     \
                    /       \
                   /         \
                  /           \
                 /             \
                /               \
             10(3)             21(3)
              / \               / \
             /   \             /   \
            /     \           /     \
           /       \         /       \
          /         \     18(1)     24(2)
         /           \       \       / \
        /             \     19(0)   /   \
      5(2)           12(2)         /     \
      / \             / \       23(1)   31(0)
     /   \           /   \       /
    /     \         /     \   22(0)
  2(1)   8(1)    11(0)   15(1)
  /       / \             /
1(0)     /   \         13(0)
        /     \
      6(0)   9(0)
--------------------

After inserting key 7..
                      16(5)
                       / \
                      /   \
                     /     \
                    /       \
                   /         \
                  /           \
                 /             \
                /               \
             10(4)             21(3)
              / \               / \
             /   \             /   \
            /     \           /     \
           /       \         /       \
          /         \     18(1)     24(2)
         /           \       \       / \
        /             \     19(0)   /   \
      5(3)           12(2)         /     \
      / \             / \       23(1)   31(0)
     /   \           /   \       /
    /     \         /     \   22(0)
  2(1)   8(2)    11(0)   15(1)
  /       / \             /
1(0)     /   \         13(0)
        /     \
      6(1)   9(0)
        \
       7(0)
--------------------

Insert a new node (+ to insert, - to delete, 0 to exit): -12
                      16(4)
                       / \
                      /   \
                     /     \
                    /       \
                   /         \
                  /           \
                 /             \
                /               \
               /                 \
             8(3)               21(3)
             / \                 / \
            /   \               /   \
           /     \             /     \
          /       \           /       \
         /         \       18(1)     24(2)
        /           \         \       / \
      5(2)         10(2)     19(0)   /   \
      / \           / \             /     \
     /   \         /   \         23(1)   31(0)
    /     \       /     \         /
  2(1)   6(1)   9(0)   13(1)   22(0)
  /         \           / \
1(0)       7(0)        /   \
                      /     \
                   11(0)   15(0)
--------------------

Insert a new node (+ to insert, - to delete, 0 to exit): 0
*/